package com.xcity.game.activity2.share.template;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xcity.game.item.ItemInfo;

import naga.x.game.template.Template;
import naga.x.game.template.TemplateException;
import naga.x.game.template.annotation.Excel;

/**
 * qq玩吧分享活动
 * @author yang.li
 *
 */
@Excel(value = "qq_share.xlsx", sheet = 0)
public class QQShareActivityTemplate extends Template {

	/** 名称 */
	protected String name;

	/** 每日次数 */
	protected int count;
	
	/** 累计次数 */
	protected int totalCount;
	
	/** 等级限制 */
	protected int levelLimit;

	/** 完成条件（参考第二个表） */
	protected byte conditionId;

	/** 条件参数 */
	protected String[] conditions;

	/** 奖励现金 */
	protected int gold;

	/** 奖励钻石 */
	protected int stone;

	/** 奖励物品1id */
	protected int[] items;
	protected List<ItemInfo> itemsWrapped;
	
	private boolean hasReward;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}
	
	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getLevelLimit() {
		return levelLimit;
	}

	public void setLevelLimit(int levelLimit) {
		this.levelLimit = levelLimit;
	}

	public byte getConditionId() {
		return conditionId;
	}

	public void setConditionId(byte conditionId) {
		this.conditionId = conditionId;
	}

	public String[] getConditions() {
		return conditions;
	}

	public void setConditions(String[] conditions) {
		this.conditions = conditions;
	}

	public int getGold() {
		return gold;
	}

	public void setGold(int gold) {
		this.gold = gold;
	}

	public int getStone() {
		return stone;
	}

	public void setStone(int stone) {
		this.stone = stone;
	}

	public int[] getItems() {
		return items;
	}

	public void setItems(int[] items) {
		this.items = items;
	}
	
	public String getCondition(int index) {
		return conditions != null && index >= 0 && index < conditions.length ? conditions[index] : null;
	}

	public List<ItemInfo> getItemList() {
		return itemsWrapped;
	}
	
	public boolean hasReward() {
		return hasReward;
	}

	@Override
	public void validate() throws TemplateException {
		if (items != null && items.length > 0) {
			itemsWrapped = new ArrayList<ItemInfo>(items.length >> 1);
			for (int i = 0; i < items.length; i += 2) {
				itemsWrapped.add(new ItemInfo(items[i], items[i + 1]));
			}
		} else {
			itemsWrapped = Collections.emptyList();
		}
		hasReward = checkReward();
	}
	
	private boolean checkReward() {
		if (gold > 0 || stone > 0) {
			return true;
		}
		for (int i = 0; i < itemsWrapped.size(); i++) {
			ItemInfo item = itemsWrapped.get(i);
			if (item.getCount() > 0) {
				return true;
			}
		}
		return false;
	}
}
